
package com.unc.allergyex.objects;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * This class extends the Android class ItemizedOverlay<OverlayItem>.
 * RestaurantPins are added to this overlay in MapSearch or MapDisambiguation.
 * This class dynamically creates balloon views when the map pins are tapped,
 * as well as animating the map view to be focused on the map pin that was tapped.
 * 
 * @author Abigail
 *
 */

public class RestaurantItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {

	private ArrayList<RestaurantPin> mOverlays = new ArrayList<RestaurantPin>();
	private MapView mapView;
	final MapController mc;
	private final static int viewOffset = 35;
	private RestaurantPin currentFocusedItem;
	private Balloon<OverlayItem> balloonView;


	/**
	 * This is the constructor for RestaurantItemizedOverlay. 
	 * It calls the superclass constructor ItemizedOverlay.
	 * It sets class variables to parameters passed to the constructor
	 * 
	 * @param defaultMarker This is the icon that will represent all overlay items in this overlay on the map.
	 * @param mView This is the mapView of the MapActivity that is instantiating this object.
	 * @param context This is the context of the MapActivity that is instantiating this object.
	 */
	public RestaurantItemizedOverlay(Drawable defaultMarker, MapView mView, Context context) {
		super(boundCenterBottom(defaultMarker));
		mapView = mView;
		mc = mapView.getController();
	}

	/**
	 * Adds a RestaurantPin (subclass of OverlayItem) to the overlay, then populates.
	 * 
	 * @param overlay The OverlayItem subclass RestaurantPin that is being added to the RestaurantItemizedOverlay
	 */
	public void addOverlay(RestaurantPin overlay){
		mOverlays.add(overlay);
		populate();
	}

	/**
	 * Default method for ItemizedOverlay.
	 * It returns the overlay item in the overlay array
	 * @see com.google.android.maps.ItemizedOverlay#createItem(int)
	 */
	@Override
	protected OverlayItem createItem(int i){
		return mOverlays.get(i);
	}
	/**
	 * Default method for ItemizedOverlay.
	 * Returns the number of items in the overlay.
	 * @see com.google.android.maps.ItemizedOverlay#size()
	 */
	@Override
	public int size(){
		return mOverlays.size();
	}
	/**
	 * This method is launched when a map pin is tapped. It
	 * launches the method that creates a balloon and animates
	 * the map to be centered at the pin that was tapped.
	 * Sets the class variable currentFocusedItem to be the
	 * RestaurantPin the map pin represents
	 * 
	 * @see com.google.android.maps.ItemizedOverlay#onTap(int)
	 */
	protected boolean onTap(int index) {
		currentFocusedItem = (RestaurantPin) createItem(index);
		createAndDisplayBalloonOverlay();
		mc.animateTo(currentFocusedItem.getPoint());
		return true;
	}

	/**
	 * Creates a new balloon object.
	 * 
	 * @return Returns a new Balloon
	 */
	protected Balloon<OverlayItem> createBalloonOverlayView() {
		return new Balloon<OverlayItem>(mapView.getContext(), getBalloonBottomOffset(), currentFocusedItem);
	}

	/**
	 * Returns the class variable viewOffSet.
	 */
	public int getBalloonBottomOffset() {
		return viewOffset;
	}

	/**
	 * Sets the visibility of the class variable balloonView to not visible.
	 */
	protected void hideBalloon() {
		if (balloonView != null) {
			balloonView.setVisibility(View.GONE);
		}
	}
	/**
	 * Hides all balloons that are not the current focused balloon
	 * @param overlays The list of all overlay items.
	 */
	private void hideOtherBalloons(List<Overlay> overlays) {
		for (Overlay overlay : overlays) {
			if (overlay instanceof RestaurantItemizedOverlay && overlay != this) {
				((RestaurantItemizedOverlay) overlay).hideBalloon();
			}
		}

	}

	/**
	 * This method creates and displays the appropriate balloons based on the current
	 * focused map pin.
	 */
	private boolean createAndDisplayBalloonOverlay(){
		boolean isRecycled;

		if (balloonView == null) {
			balloonView = createBalloonOverlayView();
			isRecycled = false;
		} 
		else {
			isRecycled = true;
		}

		balloonView.setVisibility(View.GONE);

		List<Overlay> mapOverlays = mapView.getOverlays();
		if (mapOverlays.size() > 1) {
			hideOtherBalloons(mapOverlays);
		}

		if (currentFocusedItem != null)
			balloonView.setData((RestaurantPin) currentFocusedItem);

		GeoPoint point = currentFocusedItem.getPoint();
		
		/** Setting layout parameters for map view */
		MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,MapView.LayoutParams.BOTTOM_CENTER);
		params.mode = MapView.LayoutParams.MODE_MAP;

		balloonView.setVisibility(View.VISIBLE);

		if (isRecycled) {
			balloonView.setLayoutParams(params);
		} 
		else {
			mapView.addView(balloonView, params);
		}

		return isRecycled;
	}


}
